//
//  SetTimeViewController.m
//  Clock
//
//  Created by zc on 15/8/22.
//  Copyright (c) 2015年 zc. All rights reserved.
//

#import "SetTimeViewController.h"
#define IMAGE_TAG 123412

@interface SetTimeViewController ()
@property(nonatomic,retain)UITextField * hour;
@property(nonatomic,retain)UITextField * min;
@property(nonatomic,retain)UITextField * second;
@end

@implementation SetTimeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self createTextField];
    [self createControlButton];
}


- (void)createTextField
{
    _hour = [[UITextField alloc]initWithFrame:CGRectMake(20, 50, 335, 30)];
    _hour.borderStyle = UITextBorderStyleRoundedRect;
    _hour.placeholder = @"请输入你要设置的时间/H时";
    _hour.keyboardType = UIKeyboardTypeNumberPad;
    
    _min = [[UITextField alloc]initWithFrame:CGRectMake(20, 90, 335, 30)];
    _min.borderStyle = UITextBorderStyleRoundedRect;
    _min.placeholder = @"请输入你要设置的时间/M分";
    _min.keyboardType = UIKeyboardTypeNumberPad;
    
    _second = [[UITextField alloc]initWithFrame:CGRectMake(20, 130, 335, 30)];
    _second.borderStyle = UITextBorderStyleRoundedRect;
    _second.placeholder = @"请输入你要设置的时间/S秒";
    _second.keyboardType = UIKeyboardTypeNumberPad;
    
    [self.view addSubview:_hour];
    [self.view addSubview:_second];
    [self.view addSubview:_min];
}

- (void)createControlButton
{
    NSArray * title = @[@"确定",@"返回"];
    for (int i = 0; i < 2; i++)
    {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(20 + i*175, 200, 155, 30);
        button.backgroundColor = [UIColor purpleColor];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:17];
        button.layer.cornerRadius = 5;   button.tag = i+23;
        [button setTitle:title[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }
    
    // 秒表、世界时间、
    NSArray * imageArray = @[@"stopwatch_1",@"worldtime_1"];
    for (int i = 0; i < 2; i++) {
        UIImage * image = [UIImage imageNamed:imageArray[i]];
        UIImageView * imageView = [[UIImageView alloc] initWithImage:image];
        [self.view addSubview:imageView];
        imageView.center = CGPointMake(self.view.frame.size.width / 2, 350 + i * 120);
        imageView.userInteractionEnabled = YES;
        imageView.tag = IMAGE_TAG + i;
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickIcon:)]];
    }
}


- (void)clickIcon:(UITapGestureRecognizer *)tap{
    NSInteger index;
    index = tap.view.tag - IMAGE_TAG;
    if (index == 0) {

    }
    if (index == 1) {
        NSLog(@"世界时间");
    }
}

- (void)buttonAction:(UIButton *)button
{
    NSArray * timeArray = @[_hour.text,_min.text,_second.text];
    NSUInteger tag = button.tag-23;
    switch (tag) {
        case 0:{
            if (_hour.text.length == 0 || _min.text.length == 0 || _second.text.length == 0) {
                UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:@"您没有设置时间，请重新输入" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
                [alert addAction:okAction];
                
                [self presentViewController:alert animated:YES completion:nil];
                
                return;
            }
            if (![self timeJudge]) {
                [MBProgressHUD showError:@"请输入正确的时间"];
                return;
            }

            [[NSNotificationCenter defaultCenter] postNotificationName:@"clock" object:nil userInfo:@{@"key":timeArray}];
            [self dismissViewControllerAnimated:YES completion:^{}];
            break;
        }
            
        case 1: [self dismissViewControllerAnimated:YES completion:^{}]; break;
        default:
            break;
    }
}

- (BOOL)timeJudge
{
    if ([_hour.text integerValue]>=24 || [_min.text integerValue] >= 60 || [_second.text integerValue] >= 60) {
        return NO;
    }
    return YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_hour resignFirstResponder];
    [_min resignFirstResponder];
    [_second resignFirstResponder];
}
@end
